<template>
    <div>
        <el-dialog center :title="isAdd ? '添加管理员' : '编辑管理员'" :visible.sync="isShow" :close-on-press-escape="false"
            :before-close="cancel">
            <el-form :model="user_info" :rules="rules" ref="userinfo">
                <el-form-item prop="roleid" label="所属角色" :label-width="formLabelWidth">
                    <el-select v-model="user_info.roleid" placeholder="请选择">
                        <el-option v-for="item in rolelist" :key="item.id" :value="item.id" :label="item.rolename">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="username" label="管理员名称" :label-width="formLabelWidth">
                    <el-input v-model="user_info.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input v-model="user_info.password" autocomplete="off"></el-input>
                </el-form-item>
                <p>密码为空，则不做修改</p>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="user_info.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="comfirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
// 调用接口
import { userAdd, userInfo, userEdit } from '../../request/api'
//引入辅助性函数
import { mapActions, mapGetters } from "vuex";
export default {
    props: ["isShow", "isAdd"],
    data() {
        return {
            user_info: {
                roleid: "", //角色编号
                username: "", //管理员名称
                password: "", //密码
                status: 1, //状态   1正常2禁用   number类型
            },
            formLabelWidth: "120px",
            rules: {
                username: [
                    { required: true, message: "请输入管理员名称", trigger: "blur" },
                    {
                        min: 2,
                        max: 15,
                        message: "长度在 2 到 15 个字符",
                        trigger: "blur",
                    },
                ],
                roleid: [
                    { required: true, message: "请选择上级管理员", trigger: "change" },
                ],
            },
        };
    },
    mounted() {
        if (this.rolelist.length == 0) {
            this.getRole();
        }
    },
    computed: {
        ...mapGetters({
            rolelist: 'role/getRoleList'
        })
    },
    methods: {
        ...mapActions({
            getRole: 'role/changeRoleAction',
            getUser: 'user/getUserAction',
            getCount: 'user/getCountAction'
        }),
        cancel() {
            this.$emit('cancel', false)
            this.reset()
        },
        lookup(uid) {
            userInfo({ uid }).then((res) => {
                if (res.data.code == 200) {
                    this.user_info = res.data.list;
                    this.user_info.uid = uid;
                    // 注意： 为空则不做修改！！！
                    this.user_info.password = "";
                }
            })
        },
        reset() {
            this.user_info = {
                roleid: "", //角色编号
                username: "", //管理员名称
                password: "", //密码
                status: 1, //状态   1正常2禁用   number类型
            },
                //清空表单验证
                this.$refs.userinfo.resetFields();
        },
        comfirm() {
            this.$refs.userinfo.validate((val) => {
                if (val) {
                    // 判断是编辑还是添加
                    if (this.isAdd) {
                        //调用添加接口
                        userAdd(this.user_info).then((res) => {
                            if (res.data.code == 200) {
                                //添加成功
                                this.$message.success(res.data.msg);
                                //关闭弹框
                                this.cancel();
                                //重新获取列表
                                this.getUser();
                                //重新获取总条数
                                this.getCount();
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        })

                    } else {
                        // 编辑
                        userEdit(this.user_info).then((res) => {
                            if (res.data.code == 200) {
                                //添加成功
                                this.$message.success(res.data.msg);
                                //关闭弹框
                                this.cancel();
                                //重新获取列表
                                this.getUser();
                                //重新获取总条数
                                this.getCount();
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        })
                    }
                }
            })
        }
    }
};
</script>

<style scoped>
</style>
